
package com.tools.cleanmaster.functionpage;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tools.cleanmaster.taskmain.ProcessItem;
import com.tools.cleanmaster.utils.UiUtils;

import java.util.HashMap;
import java.util.List;

public class AppIconHorizontal extends RelativeLayout {

    class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
        private List<ProcessItem> mList;

        public ListViewAdapter(List<ProcessItem> list) {
            mList = list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(com.tools.cleanmaster.R.layout.function_appsicon_item, viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int i) {
            viewHolder.iconView.setImageDrawable(mList.get(i).icon);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public void clear() {
            mList.clear();
            notifyDataSetChanged();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView iconView;

            public ViewHolder(View view) {
                super(view);
                iconView = view.findViewById(com.tools.cleanmaster.R.id.icon_iv);
            }
        }
    }

    private RecyclerView mRecyclerView;
    private ListViewAdapter mAdapter;
    private Animation mFlipAnimation;
    private LayoutAnimationController mFlipAnimationController;
    private Context mContext;
    private List<ProcessItem> mProcessItems;
    private ImageView showDetailsArrow;

    public AppIconHorizontal(Context ctx) {
        super(ctx);
        init();
    }

    public AppIconHorizontal(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init();
    }

    private void init() {
        mContext = getContext();
        inflate(getContext(), com.tools.cleanmaster.R.layout.function_appicon_horizontal, this);
        showDetailsArrow = findViewById(com.tools.cleanmaster.R.id.show_details_arrow);
        mFlipAnimation = AnimationUtils.loadAnimation(mContext, com.tools.cleanmaster.R.anim.appicon_out_anim);
        mFlipAnimation.setFillAfter(true);
        mFlipAnimationController = new LayoutAnimationController(mFlipAnimation);
        mFlipAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
        mFlipAnimationController.setDelay(0.4f);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        mRecyclerView = findViewById(com.tools.cleanmaster.R.id.appicon_recycler);
    }

    public void clearAppAnimation(final Animation.AnimationListener listener) {
        if(mAdapter != null && mAdapter.getItemCount() == 0){
            listener.onAnimationEnd(null);
            return;
        }
        mRecyclerView.setLayoutAnimation(mFlipAnimationController);
        mRecyclerView.setLayoutAnimationListener(listener);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.notifyDataSetChanged();
                mRecyclerView.startLayoutAnimation();
            }
        }, 50);
    }

    public void setAppsIcons(List<ProcessItem> processItems) {
        mProcessItems = processItems;
        mAdapter = new ListViewAdapter(mProcessItems);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, 0, false));
        mRecyclerView.setHasFixedSize(true);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<String, Integer>();
        stringIntegerHashMap.put(SpaceItemDecoration.LEFT_DECORATION, UiUtils.dipToPx(mContext, 22));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(stringIntegerHashMap));
        mRecyclerView.setAdapter(mAdapter);
    }

    public void setIsNeedShowArrow(boolean isNeed) {
        showDetailsArrow.setVisibility(isNeed ? VISIBLE : GONE);
    }

    public void setArrowClickListener(OnClickListener listener) {
        showDetailsArrow.setOnClickListener(listener);
    }
}
